package com.kuang.service.user;

import com.kuang.dao.BaseDao;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.User;
import com.kuang.utils.PageSupport;

import java.sql.Connection;
import java.util.List;

public class UserServiceImpl implements UserService{

//    业务层都要使用dao，所有要引用dao
    private UserDao userDao;
    public UserServiceImpl() {
       userDao = new UserDaoImpl();
    }


    public User login(String userCode, String userPassword) {
        Connection connection=null;
        User user =null;

        connection = BaseDao.getConnect();
//        System.out.println(connection);
        user=userDao.login(connection, userCode);
        if(user!=null && user.getUserPassword().equals(userPassword)){
                return user;
        }

        return null;
    }

    public Boolean updatePassword(int id, String newPassword) {
        Connection connection = null;
        boolean flag = false;
        int updateRow=0;

        connection=BaseDao.getConnect();
        updateRow=userDao.updatePassword(connection,id,newPassword);
        if(updateRow>0){
            flag=true;
        }

        return flag ;
    }

    public List<User> getUserList(String userName, String roleCode, int pageSize, int currentPageNo) {
        Connection connection = BaseDao.getConnect();
        PageSupport page = new PageSupport();
        List<User> userList = null;

//        设置分页
//         page.setPageSize(pageSize);
//        pageSize = page.getPageSize();
//
//         page.setCurrentPageNo(currentPageNo);
//         currentPageNo = page.getCurrentPageNo();
        currentPageNo =currentPageNo -1;
        userList=userDao.getUserList(connection,userName,roleCode,pageSize,currentPageNo);

        return userList;
    }

    public List<User> getUserTotal(String userName, String roleCode) {
        Connection connection = BaseDao.getConnect();
        List<User> userList =userDao.getUserTotal(connection,userName,roleCode);

        return userList;
    }

//    public int getTotalCount() {
//        int totalCount=0;
//        Connection connection=BaseDao.getConnect();
//        totalCount=userDao.getTotalCount(connection);
//        return totalCount;
//    }
}
